<template>
  <div v-loading.lock="loading" class="base-content">
    <div class="basic-info">
      <el-form ref="queryForm" :inline="true" :model="queryForm">
        <el-form-item :label="dataList.ProgrammeNumber" prop="num">
          <el-input v-model="queryForm.num" :placeholder="dataList['NO.']" />
        </el-form-item>
        <el-form-item :label="dataList.ProgrammeName" prop="name">
          <el-input
            v-model="queryForm.name"
            :placeholder="dataList.ProgrammeName"
          />
        </el-form-item>
        <el-form-item :label="dataList.Status" prop="status">
          <el-select
            v-model="queryForm.status"
            :placeholder="dataList.Placeholder"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="dataList.ProgrammeName" prop="preparedDate">
          <el-date-picker
            v-model="queryForm.preparedDate"
            type="daterange"
            :range-separator="dataList.To"
            :start-placeholder="dataList.StartDate"
            :end-placeholder="dataList.EndDate"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item :label="dataList.JoinDevice" prop="deviceNameNum">
          <el-input
            v-model="queryForm.deviceNameNum"
            :placeholder="dataList.EnterDevice"
          />
        </el-form-item>
      </el-form>

      <div class="base-content-operate">
        <el-button type="primary" @click="onQuery">{{ btn.Query }}</el-button>
        <el-button @click="cancelForm">{{ btn.Reset }}</el-button>
        <el-button icon="el-icon-plus" type="primary" @click="onNew">{{
          btn.Add
        }}</el-button>
      </div>

      <el-table
        :data="list"
        element-loading-text="Loading"
        fit
        highlight-current-row
      >
        <el-table-column align="center" :label="dataList.Number" width="80">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.ProgrammeNumber">
          <template slot-scope="scope">
            <el-link type="primary" @click="toView(scope.row)">
              {{ scope.row.num }}</el-link
            >
          </template>
        </el-table-column>
        <el-table-column :label="dataList.ProgrammeName">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.Status">
          <template slot-scope="scope">
            {{ scope.row.statusName }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.PreparedName">
          <template slot-scope="scope">
            {{ scope.row.preparedUser.name }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.ProgrammeName">
          <template slot-scope="scope">
            {{ scope.row.preparedDate }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.Operation">
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link
                v-if="scope.row.status == 10 || scope.row.status == 40"
                size="mini"
                type="primary"
                @click="handleEdit(scope.row)"
                >{{ btn.Edit }}</el-link
              >
              <el-link
                v-if="scope.row.status == 10 || scope.row.status == 40"
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
                >{{ btn.Nullify }}</el-link
              >
              <el-link
                v-if="scope.row.status == 30"
                size="mini"
                type="primary"
                @click="handlePlan(scope.row)"
                >{{ dataList.GeneratePlan }}</el-link
              >
            </div>
          </template>
        </el-table-column>
      </el-table>

      <BasePagination
        :total="pagination.total"
        :current-page="pagination.pageIndex"
        :page-count="pagination.total"
        @changePage="changePage"
        @sizeChange="sizeChange"
      />
    </div>

    <DeviceVerificationAddDialog
      v-if="dialogFormAddVisible"
      ref="DeviceVerificationAddDialog"
      :dialog-form-visible.sync="dialogFormAddVisible"
    />
    <DeviceVerificationEditDialog
      v-if="dialogFormEditVisible"
      :id.sync="id"
      ref="DeviceVerificationEditDialog"
      :dialog-form-visible.sync="dialogFormEditVisible"
    />
    <DeviceVerificationViewDialog
      v-if="dialogFormViewVisible"
      :id.sync="id"
      ref="DeviceVerificationViewDialog"
      :dialog-form-visible.sync="dialogFormViewVisible"
    />
    <DeviceVerificationPlanAddDialog
      v-if="dialogDeviceVerificationPlanAddVisible"
      ref="DeviceVerificationPlanAddDialog"
      :dialog-form-visible.sync="dialogDeviceVerificationPlanAddVisible"
      :from="1"
      :verification-id.sync="id"
    />
  </div>
</template>

<script>
import { getList, update } from "../../../api/DeviceVerification";
import DeviceVerificationAddDialog from "./DeviceVerificationAddDialog.vue";
import DeviceVerificationEditDialog from "./DeviceVerificationEditDialog.vue";
import DeviceVerificationViewDialog from "./DeviceVerificationViewDialog.vue";
import DeviceVerificationPlanAddDialog from "../DeviceVerificationPlan/DeviceVerificationPlanAddDialog.vue";
import { deviceCalibrationStatus } from "../../../utils/const";
import { formatTimeMin, formatTimeMax } from "../../../utils/time";
export default {
  name: "DeviceVerificationList",
  components: {
    DeviceVerificationAddDialog,
    DeviceVerificationEditDialog,
    DeviceVerificationViewDialog,
    DeviceVerificationPlanAddDialog,
  },
  data() {
    return {
      id: 0,
      loading: true,
      dialogDeviceVerificationPlanAddVisible: false,
      dialogFormEditVisible: false,
      dialogFormViewVisible: false,
      list: [],
      options: deviceCalibrationStatus,
      queryForm: {
        num: "",
        name: "",
        status: -1,
        preparedDate: [],
        device: "",
        stateTime: null,
        endTime: null,
        departId: sessionStorage.getItem("currentDepartmentId"),
      },
      dialogFormAddVisible: false,
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  computed: {
    btn() {
      return this.$t("ModuleDevice.buttons");
    },
    dataList() {
      return this.$t("ModuleDevice.Device");
    },
  },
  created() {
    this.getList();
  },
  methods: {
    toView(row) {
      this.id = row.id;
      this.dialogFormViewVisible = true;
    },
    changePage(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    cancelForm() {
      this.$refs.queryForm.resetFields();
      this.pagination.pageIndex = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      this.queryForm.stateTime = formatTimeMin(
        this.queryForm.preparedDate[0] ? this.queryForm.preparedDate[0] : null
      );
      this.queryForm.endTime = formatTimeMax(
        this.queryForm.preparedDate[1] ? this.queryForm.preparedDate[1] : null
      );
      getList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    onNew() {
      this.dialogFormAddVisible = true;
    },
    handlePlan(rowData) {
      this.id = rowData.id;
      this.dialogDeviceVerificationPlanAddVisible = true;
    },
    handleDelete(rowData) {
      this.$confirm(
        this.$t("ModuleDevice.Message.ConfirmNullify"),
        this.$t("ModuleDevice.buttons.Tips"),
        {
          confirmButtonText: this.$t("ModuleDevice.buttons.Confirm"),
          cancelButtonText: this.$t("ModuleDevice.buttons.Cancel"),
          type: "warning",
          center: true,
        }
      )
        .then(() => {
          rowData.status = "70";
          rowData.statusName = this.dataList.Invalid;
          update(rowData).then((res) => {});
        })
        .catch(() => {});
    },
    handleEdit(rowData) {
      this.id = rowData.id;
      this.dialogFormEditVisible = true;
    },
    onQuery() {
      this.pagination.pageIndex = 1;
      this.getList();
    },
  },
};
</script>
